#!/usr/bin/env bash
pwd=$PWD
sudo apt install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -y
sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev -y
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev -y
sudo apt install libxvidcore-dev libx264-dev libatlas-base-dev gfortran ffmpeg -y

mkdir -p ~/ct && cd ~/ct

# refer to https://www.learnopencv.com/install-opencv3-on-ubuntu/
git clone https://github.com/opencv/opencv.git --branch=3.4 --depth=1
git clone https://github.com/opencv/opencv_contrib.git --branch=3.4 --depth=1

cd opencv
rm -rf build
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=OFF \
      -DBUILD_TIFF=ON ..

make -j8
sudo make install

sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
cd $pwd

# https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/